use crate::src::src::compare_int::{int_compare, int_equal};
use crate::src::src::sortedarray::{
    sortedarray_free, sortedarray_get, sortedarray_index_of, sortedarray_insert,
    sortedarray_length, sortedarray_new, sortedarray_remove, sortedarray_remove_range,
};
use crate::src::struct_and_type::{
    SortedArray, SortedArrayEqualFunc, SortedArrayValue, UnitTestFunction,
};
use crate::src::test::alloc_testing::{alloc_test_free, alloc_test_malloc, alloc_test_set_limit};
use crate::src::test::framework::run_tests;
use :: libc;
extern "C" {
    fn rand() -> i32;
}
#[no_mangle]
pub extern "C" fn check_sorted_prop(mut sortedarray: *mut SortedArray) {
    let mut i: u32 = 0;
    i = 1 as i32 as u32;
    while i < sortedarray_length(sortedarray) {
        assert!(
            int_compare(
                sortedarray_get(sortedarray, i.wrapping_sub(1 as i32 as u32),) as *mut libc::c_void,
                sortedarray_get(sortedarray, i) as *mut libc::c_void,
            ) <= 0 as i32
        );
        i = i.wrapping_add(1);
        i;
    }
}
#[no_mangle]
pub extern "C" fn free_sorted_ints(mut sortedarray: *mut SortedArray) {
    let mut i: u32 = 0;
    i = 0 as i32 as u32;
    while i < sortedarray_length(sortedarray) {
        let mut pi: *mut i32 = sortedarray_get(sortedarray, i) as *mut i32;
        alloc_test_free(pi as *mut libc::c_void);
        i = i.wrapping_add(1);
        i;
    }
    sortedarray_free(sortedarray);
}
#[no_mangle]
pub extern "C" fn generate_sortedarray_equ(mut equ_func: SortedArrayEqualFunc) -> *mut SortedArray {
    let mut sortedarray: *mut SortedArray = 0 as *mut SortedArray;
    let mut i: u32 = 0;
    let mut array: [i32; 20] = [
        10 as i32,
        12 as i32,
        12 as i32,
        1 as i32,
        2 as i32,
        3 as i32,
        6 as i32,
        7 as i32,
        2 as i32,
        23 as i32,
        13 as i32,
        23 as i32,
        23 as i32,
        34 as i32,
        31 as i32,
        9 as i32,
        21 as i32,
        -(2 as i32),
        -(12 as i32),
        -(4 as i32),
    ];
    sortedarray = sortedarray_new(
        0 as i32 as u32,
        equ_func,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    i = 0 as i32 as u32;
    while i < 20 as i32 as u32 {
        let mut pi: *mut i32 = alloc_test_malloc(::core::mem::size_of::<i32>() as u64) as *mut i32;
        (unsafe { *pi = array[i as usize] });
        sortedarray_insert(sortedarray, pi as SortedArrayValue);
        i = i.wrapping_add(1);
        i;
    }
    return sortedarray;
}
#[no_mangle]
pub extern "C" fn generate_sortedarray() -> *mut SortedArray {
    return generate_sortedarray_equ(Some(
        int_equal as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32,
    ));
}
#[no_mangle]
pub extern "C" fn test_sortedarray_new_free() {
    let mut sortedarray: *mut SortedArray = 0 as *mut SortedArray;
    sortedarray = sortedarray_new(
        0 as i32 as u32,
        Some(int_equal as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    assert!(!sortedarray.is_null());
    sortedarray_free(sortedarray);
    sortedarray_free(0 as *mut SortedArray);
    alloc_test_set_limit(0 as i32);
    sortedarray = sortedarray_new(
        0 as i32 as u32,
        Some(int_equal as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    assert!(sortedarray.is_null());
    alloc_test_set_limit(-(1 as i32));
}
#[no_mangle]
pub extern "C" fn test_sortedarray_insert() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut i: u32 = 0;
    i = 0 as i32 as u32;
    while i < 20 as i32 as u32 {
        let mut i_0: i32 =
            ((unsafe { rand() }) as f32 / 2147483647 as i32 as f32 * 100 as i32 as f32) as i32;
        let mut pi: *mut i32 = alloc_test_malloc(::core::mem::size_of::<i32>() as u64) as *mut i32;
        (unsafe { *pi = i_0 });
        sortedarray_insert(sortedarray, pi as SortedArrayValue);
        i = i.wrapping_add(1);
        i;
    }
    check_sorted_prop(sortedarray);
    free_sorted_ints(sortedarray);
}
#[no_mangle]
pub extern "C" fn test_sortedarray_remove() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut ip: *mut i32 = sortedarray_get(sortedarray, (15 as i32 + 1 as i32) as u32) as *mut i32;
    let mut i: i32 = unsafe { *ip };
    alloc_test_free(sortedarray_get(sortedarray, 15 as i32 as u32) as *mut i32 as *mut libc::c_void);
    sortedarray_remove(sortedarray, 15 as i32 as u32);
    assert!((unsafe { *(sortedarray_get(sortedarray, 15 as i32 as u32) as *mut i32) }) == i);
    check_sorted_prop(sortedarray);
    free_sorted_ints(sortedarray);
}
#[no_mangle]
pub extern "C" fn test_sortedarray_remove_range() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut new: [i32; 4] = [0; 4];
    let mut i: u32 = 0;
    i = 0 as i32 as u32;
    while i < 4 as i32 as u32 {
        new[i as usize] = unsafe {
            *(sortedarray_get(sortedarray, ((7 as i32 + 4 as i32) as u32).wrapping_add(i))
                as *mut i32)
        };
        i = i.wrapping_add(1);
        i;
    }
    i = 0 as i32 as u32;
    while i < 4 as i32 as u32 {
        alloc_test_free(
            sortedarray_get(sortedarray, (7 as i32 as u32).wrapping_add(i)) as *mut i32
                as *mut libc::c_void,
        );
        i = i.wrapping_add(1);
        i;
    }
    sortedarray_remove_range(sortedarray, 7 as i32 as u32, 4 as i32 as u32);
    i = 0 as i32 as u32;
    while i < 4 as i32 as u32 {
        assert!(
            (unsafe {
                *(sortedarray_get(sortedarray, (7 as i32 as u32).wrapping_add(i)) as *mut i32)
            }) == new[i as usize]
        );
        i = i.wrapping_add(1);
        i;
    }
    check_sorted_prop(sortedarray);
    free_sorted_ints(sortedarray);
}
#[no_mangle]
pub extern "C" fn test_sortedarray_index_of() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut i: u32 = 0;
    i = 0 as i32 as u32;
    while i < 20 as i32 as u32 {
        let mut r: i32 = sortedarray_index_of(
            sortedarray,
            sortedarray_get(sortedarray, i) as SortedArrayValue,
        );
        assert!(r >= 0 as i32);
        assert!(
            (unsafe { *(sortedarray_get(sortedarray, r as u32) as *mut i32) })
                == (unsafe { *(sortedarray_get(sortedarray, i) as *mut i32) })
        );
        i = i.wrapping_add(1);
        i;
    }
    free_sorted_ints(sortedarray);
}
extern "C" fn ptr_equal(mut v1: SortedArrayValue, mut v2: SortedArrayValue) -> i32 {
    return (v1 == v2) as i32;
}
#[no_mangle]
pub extern "C" fn test_sortedarray_index_of_equ_key() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray_equ(Some(
        ptr_equal as unsafe extern "C" fn(SortedArrayValue, SortedArrayValue) -> i32,
    ));
    let mut i: u32 = 0;
    i = 0 as i32 as u32;
    while i < 20 as i32 as u32 {
        let mut r: i32 = sortedarray_index_of(
            sortedarray,
            sortedarray_get(sortedarray, i) as SortedArrayValue,
        );
        assert!(r >= 0 as i32);
        assert!(i == r as u32);
        i = i.wrapping_add(1);
        i;
    }
    free_sorted_ints(sortedarray);
}
#[no_mangle]
pub extern "C" fn test_sortedarray_get() {
    let mut i: u32 = 0;
    let mut arr: *mut SortedArray = generate_sortedarray();
    i = 0 as i32 as u32;
    while i < sortedarray_length(arr) {
        assert!(sortedarray_get(arr, i) == sortedarray_get(arr, i));
        assert!(
            (unsafe { *(sortedarray_get(arr, i) as *mut i32) })
                == (unsafe { *(sortedarray_get(arr, i) as *mut i32) })
        );
        i = i.wrapping_add(1);
        i;
    }
    free_sorted_ints(arr);
}
static mut tests_test_sortedarray: [UnitTestFunction; 8] = {
    [
        Some(test_sortedarray_new_free as unsafe extern "C" fn() -> ()),
        Some(test_sortedarray_insert as unsafe extern "C" fn() -> ()),
        Some(test_sortedarray_remove as unsafe extern "C" fn() -> ()),
        Some(test_sortedarray_remove_range as unsafe extern "C" fn() -> ()),
        Some(test_sortedarray_index_of as unsafe extern "C" fn() -> ()),
        Some(test_sortedarray_index_of_equ_key as unsafe extern "C" fn() -> ()),
        Some(test_sortedarray_get as unsafe extern "C" fn() -> ()),
        None,
    ]
};
fn main_0_test_sortedarray(mut _argc: i32, mut _argv: *mut *mut i8) -> i32 {
    run_tests(unsafe { tests_test_sortedarray.as_mut_ptr() });
    return 0 as i32;
}
pub fn main_test_sortedarray() {
    let mut args: Vec<*mut i8> = Vec::new();
    for arg in ::std::env::args() {
        args.push(
            (::std::ffi::CString::new(arg))
                .expect("Failed to convert argument into CString.")
                .into_raw(),
        );
    }
    args.push(::core::ptr::null_mut());
    {
        ::std::process::exit(main_0_test_sortedarray(
            (args.len() - 1) as i32,
            args.as_mut_ptr() as *mut *mut i8,
        ) as i32)
    }
}
